package com.ruoyi.text.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.xbatis.core.mybatis.mapper.context.Pager;
import cn.xbatis.core.sql.executor.Where;
import cn.xbatis.core.sql.executor.chain.QueryChain;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.text.domain.TestDemo;
import com.ruoyi.text.domain.bo.TestDemoBo;
import com.ruoyi.text.domain.vo.TestDemoVo;
import com.ruoyi.text.mapper.TestDemoMapper;
import com.ruoyi.text.service.ITestDemoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 测试单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-20
 */
@RequiredArgsConstructor
@Service
public class TestDemoServiceImpl implements ITestDemoService {

    private final TestDemoMapper baseMapper;

    /**
     * 查询测试单
     */
    @Override
    public TestDemoVo queryById(Long id) {
        TestDemoVo TestDemovo = new TestDemoVo();
        BeanCopyUtils.copy(baseMapper.getById(id), TestDemovo);
        return TestDemovo;
    }

    /**
     * 查询测试单列表
     */
    @Override
    public TableDataInfo<TestDemoVo> queryPageList(TestDemoBo bo, PageQuery pageQuery) {
        Where lqw = buildQueryWhere(bo);
        Pager<TestDemoVo> result = QueryChain.of(baseMapper, lqw).returnType(TestDemoVo.class).paging(Pager.of(pageQuery.getPageNum(), pageQuery.getPageSize()));
        return TableDataInfo.builds(result);
    }

    /**
     * 查询测试单列表
     */
    @Override
    public List<TestDemoVo> queryList(TestDemoBo bo) {
        Where lqw = buildQueryWhere(bo);
        return QueryChain.of(baseMapper, lqw).returnType(TestDemoVo.class).list();
    }

    private Where buildQueryWhere(TestDemoBo bo) {
        Where where = new Where();
        where.eq(bo.getDeptId() != null, TestDemo::getDeptId, bo.getDeptId());
        where.eq(bo.getUserId() != null, TestDemo::getUserId, bo.getUserId());
        where.eq(bo.getOrderNum() != null, TestDemo::getOrderNum, bo.getOrderNum());
        where.eq(StringUtils.isNotBlank(bo.getTestKey()), TestDemo::getTestKey, bo.getTestKey());
        where.eq(StringUtils.isNotBlank(bo.getValue()), TestDemo::getValue, bo.getValue());
        return where;
    }

    /**
     * 新增测试单
     */
    @Override
    public Boolean insertByBo(TestDemoBo bo) {
        TestDemo add = BeanUtil.toBean(bo, TestDemo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.save(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改测试单
     */
    @Override
    public Boolean updateByBo(TestDemoBo bo) {
        TestDemo update = BeanUtil.toBean(bo, TestDemo.class);
        validEntityBeforeSave(update);
        return baseMapper.update(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TestDemo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除测试单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
